/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Registro.java
 *
 * Created on 27-oct-2010, 15:46:59
 */

package app;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import com.digitalpersona.onetouch.*;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author null
 */
public class RegistroHuella extends javax.swing.JDialog {

    /** Creates new form Registro */
    public RegistroHuella(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        setResizable(false);
        CargaCombos();
        BOXPersona.setSelectedIndex(-1);
        BOXMano.setSelectedIndex(-1);
        BOXDedo.setSelectedIndex(-1);
    }

    private String Datos_Persona = "SELECT id, concat(nombre,' ', apellido) FROM persona WHERE estado = 1";
    private String Datos_Mano = "SELECT id, nombre_mano FROM mano";
    private String Datos_Dedo = "SELECT id, nombre_dedo FROM dedo";

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        BOXPersona = new javax.swing.JComboBox();
        BOXMano = new javax.swing.JComboBox();
        BOXDedo = new javax.swing.JComboBox();
        ButtonRegistrarHuella = new javax.swing.JButton();
        ButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registro de huella");

        jLabel19.setFont(new java.awt.Font("Arial Black", 0, 14));
        jLabel19.setText("Persona");

        jLabel20.setFont(new java.awt.Font("Arial Black", 0, 14));
        jLabel20.setText("Mano");

        jLabel21.setFont(new java.awt.Font("Arial Black", 0, 14));
        jLabel21.setText("Dedo");

        BOXPersona.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        BOXMano.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        BOXDedo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        ButtonRegistrarHuella.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        ButtonRegistrarHuella.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/apply.png"))); // NOI18N
        ButtonRegistrarHuella.setText("Registrar");
        ButtonRegistrarHuella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonRegistrarHuellaActionPerformed(evt);
            }
        });

        ButtonCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        ButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        ButtonCancelar.setText("Cancelar");
        ButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(33, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel20)
                        .addGap(39, 39, 39)
                        .addComponent(BOXMano, 0, 232, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(BOXDedo, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel19)
                        .addGap(18, 18, 18)
                        .addComponent(BOXPersona, 0, 232, Short.MAX_VALUE)))
                .addGap(59, 59, 59))
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(ButtonRegistrarHuella)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(73, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(BOXPersona, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(BOXMano, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel21)
                    .addComponent(BOXDedo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonRegistrarHuella, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ButtonRegistrarHuellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonRegistrarHuellaActionPerformed
        //controlar que haya seleccionado una persona, una mano y una huella

        if(BOXPersona.getSelectedItem() == null){           
            Message.showErrorMessage(this, "Se debe seleccionar a una persona");
            BOXPersona.requestFocus();
            return;
        }
        if(BOXMano.getSelectedItem() == null){
            Message.showErrorMessage(this, "Se debe seleccionar la mano del dedo de la huella");
            BOXMano.requestFocus();
            return;
        }
        if(BOXDedo.getSelectedItem() == null){
            Message.showErrorMessage(this, "Se debe seleccionar el dedo de la huella");
            BOXDedo.requestFocus();
            return;
        }
        //aqui controlar que todo este seleccionado y realizar el proceso de grabado
        //obtenemos lo que se selecciono
        Persona persona  = (Persona) BOXPersona.getSelectedItem();
        Mano mano = (Mano) BOXMano.getSelectedItem();
        Dedo dedo = (Dedo) BOXDedo.getSelectedItem();
        try {
            //si la huella para esa persona ya existe, deberia hacer un update
            FingerPrint.saveFingerPrint(persona.getIdPersona(), mano.getIdMano(), dedo.getIdDedo());
        } catch (Exception ex) {
            Message.showErrorMessage(this, ex.getMessage());
            ex.printStackTrace();
        }   
}//GEN-LAST:event_ButtonRegistrarHuellaActionPerformed

    private void ButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_ButtonCancelarActionPerformed

    private void CargaCombos(){
        try {
            Connection con = dbo.DbMySQLConnection.DbConnect();
            PreparedStatement ps = con.prepareStatement(Datos_Persona);
            ResultSet rs = ps.executeQuery();

            BOXPersona.removeAllItems();
            // CARGA BOX PERSONA
            while (rs.next()) {
                Persona datos = new Persona();
                datos.setIdPersona(rs.getInt(1));
                datos.setNombre(rs.getString(2));
                BOXPersona.addItem(datos);
            }

            BOXMano.removeAllItems();
            ps = con.prepareStatement(Datos_Mano);
            rs = ps.executeQuery();
            // CARGA BOX MANO
            while (rs.next()) {
                Mano datos = new Mano();
                datos.setIdMano(rs.getInt(1));
                datos.setMano(rs.getString(2));
                BOXMano.addItem(datos);
            }

            BOXDedo.removeAllItems();
            ps = con.prepareStatement(Datos_Dedo);
            rs = ps.executeQuery();
            // CARGA BOX DEDO
            while (rs.next()) {
                Dedo datos = new Dedo();
                datos.setIdDedo(rs.getInt(1));
                datos.setDedo(rs.getString(2));
                BOXDedo.addItem(datos);
            }

        } catch (SQLException ex) {
            Message.showErrorMessage(this, ex.getMessage());
            ex.printStackTrace();
        }finally{
            dbo.DbMySQLConnection.DbDisconnect();
        }
    }   


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox BOXDedo;
    private javax.swing.JComboBox BOXMano;
    private javax.swing.JComboBox BOXPersona;
    private javax.swing.JButton ButtonCancelar;
    private javax.swing.JButton ButtonRegistrarHuella;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    // End of variables declaration//GEN-END:variables

}
